package benchmarks.tpch;

import api.Configuration;
import api.Worker;
import api.connection.DBConnection;
import api.result.Result;
import api.result.ResultKV;
import api.result.UnitResult;

public abstract class TPCHWorker extends Worker {
    public TPCHWorker(Configuration config) {
        super(config);
    }

    public abstract TPCHQuery makeQueryImpl(String queryName);

    /***
     * execute specific procedure
     * @param nextProcedure procedure name: "Q1", "Q8", "Q15", "Q20", "Q21", "Q22"
     * @return
     */
    public Result executeProcedure(DBConnection connection, String nextProcedure) {
        TPCHQuery query = makeQueryImpl(nextProcedure);
        long start = System.currentTimeMillis();
        Result result = query.run(connection);
        long end = System.currentTimeMillis();
        ResultKV<UnitResult> ret = new ResultKV<>(
                config.getBenchmark() + "\t" + config.getDb_name() + "\t" + config.getScaleFactor() + "\tT" + config.getTerminals() + "\t" + nextProcedure,
                new UnitResult(end - start));
        return ret.union(result);
    }
}
